import java.util.Stack;

/**
 * @Author @fiber-cloud
 * @Date 2025/8/3 9:56
 * 设计一个支持push，pop，peek操作，并能在常数时间内检索到最小元素的栈
 */
public class MinStack {


    private Stack<Integer> stack1;
    private Stack<Integer> minStack;

    public MinStack(){
       stack1 = new Stack<>();
       minStack = new Stack<>();
    }
    //push：
    // 1.普通栈中一定放，最小栈是否放，取决于当前元素x有没有最小栈的元素小
    // 2.如果要存放的值为x，最小栈的栈顶为y x<y放  x==y放  x>y不放
    public void push(int val){
        stack1.push(val);

        if (minStack.empty()) {
            minStack.push(val);
        }else {
            int x = minStack.peek();
            if (val < x || val == x){
                minStack.push(val);
            }
        }
    }

    //弹出逻辑
    public void pop(){
        if (!stack1.empty()){
            int x = stack1.pop();
            if (x == minStack.peek()){
                minStack.pop();
            }
        }

    }

    //只针对普通栈
    public int top(){
        if (!stack1.empty()){
            return stack1.peek();
        }
        return -1;
    }
    //针对minStack
    public int getMin(){
        if (minStack.empty()){
            return -1;
        }
        return minStack.peek();
    }

}
